package com.twb.auth.controller;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.twb.auth.common.TwbLoginUser;
import com.twb.auth.entity.twbPlatform.TwbPlatform;
import com.twb.auth.entity.twbPlatform.dto.GetTwbPlatformByIdDto;
import com.twb.auth.entity.twbPlatform.dto.ListTwbPlatformDto;
import com.twb.auth.entity.twbPlatform.dto.SearchTwbPlatformDto;
import com.twb.auth.entity.twbPlatform.vo.ListTwbPlatformVo;
import com.twb.auth.entity.twbPlatform.vo.SaveTwbPlatformVo;
import com.twb.auth.entity.twbPlatform.vo.SearchTwbPlatformVo;
import com.twb.auth.entity.twbPlatform.vo.UpdateTwbPlatformByIdVo;
import com.twb.auth.service.ITwbPlatformService;
import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.security.TwbSecurityUtil;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.Results;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/** 
 * @Title  TwbPlatformController
 * @Description 平台信息
 
 * @author DSY
 * @date 2020-11-04 17:55:18
 */ 
@RestController
@RequestMapping("/system/twbPlatform")
@Api(tags = "平台信息",value="TwbPlatformController")
public class TwbPlatformController extends BaseController{
	@Autowired
	ITwbPlatformService twbPlatformService;
	@Autowired
	TwbSecurityUtil twbSecurityUtil;
	
	/**
	 * @Title  search
	 * @Description 分页查询
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2020-11-04 17:55:18
	 */
	@Auth(verifyToken = true)
	@GetMapping("/search")
	@ApiOperation(value="分页查询")
	public Result<TwbPageInfo<SearchTwbPlatformDto>> search(SearchTwbPlatformVo vo){
		TwbPageInfo<SearchTwbPlatformDto> result = twbPlatformService.searchTwbPlatform(vo);
		return Results.successWithData(result);
	}
	
	/**
	 * @Title  getById
	 * @Description 根据ID获取详情
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2020-11-04 17:55:18
	 */
	@Auth(verifyToken = true)
	@GetMapping("/getById")
	@ApiOperation(value="根据ID获取详情")
	public Result<GetTwbPlatformByIdDto> getById(
			@NotBlank
			@RequestParam
			@ApiParam(value="主键",name="id",required=true)
			String id){
		TwbPlatform twbPlatform = twbPlatformService.getById(id);
		if(twbPlatform == null) {
			return Results.failure("数据不存在");
		}
		GetTwbPlatformByIdDto dto = new GetTwbPlatformByIdDto();
		BeanUtils.copyNotNullProperties(twbPlatform, dto);
		return Results.successWithData(dto);
	}
	
	/**
	 * @Title  save
	 * @Description 保存信息
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2020-11-04 17:55:18
	 */
	
	@Auth(verifyToken = true)
	@PostMapping("/save")
	@ApiOperation(value="保存信息")
	public Result<String> save(@RequestBody @Valid SaveTwbPlatformVo vo){
		boolean result = twbPlatformService.saveTwbPlatform(vo);
		if(result) {
			return Results.success("保存成功");
		}else {
			return Results.failure("保存失败");
		}
	}
	
	
	/**
	 * @Title  updateById
	 * @Description 更新信息
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2020-11-04 17:55:18
	 */
	
	@Auth(verifyToken = true)
	@PutMapping("/updateById")
	@ApiOperation(value="更新信息")
	public Result<String> updateById(@RequestBody @Valid UpdateTwbPlatformByIdVo vo){
		boolean result = twbPlatformService.updateTwbPlatformById(vo);
		if(result) {
			return Results.success("更新成功");
		}else {
			return Results.failure("更新失败");
		}
	}
	
	
	/**
	 * @Title  deleteById
	 * @Description 删除信息
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2020-11-04 17:55:18
	 */
	
	@Auth(verifyToken = true)
	@PostMapping("/deleteById")
	@ApiOperation(value="删除信息")
	public Result<String> deleteById(
			@NotBlank
			@RequestParam
			@ApiParam(value="主键",name="id",required=true)
			String id){
		boolean result = twbPlatformService.deleteTwbPlatformById(id);
		if(result) {
			return Results.success("删除成功");
		}else {
			return Results.failure("删除失败");
		}
	}
	
	
	/**
	 * @Title  deleteBatchByIds
	 * @Description 批量删除信息
	 * @param ids
	 * @return
	 * @author DSY
	 * @date 2020-11-04 17:55:18
	 */
	
	@Auth(verifyToken = true)
	@PostMapping("/batchDeleteByIds")
	@ApiOperation(value="批量删除信息")
	public Result<String> deleteBatchByIds(
			@NotBlank
			@RequestParam
			@ApiParam(value="主键",name="id",required=true)
			String ids){
		boolean result = twbPlatformService.deleteTwbPlatformByIds(ids);
		if(result) {
			return Results.success("删除成功");
		}else {
			return Results.failure("删除失败");
		}
	}
	
	
	/**
	 * @Title  logicDeleteById
	 * @Description 逻辑删除信息
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2020-11-04 17:55:18
	 */
	
	@Auth(verifyToken = true)
	@PostMapping("/logicDeleteById")
	@ApiOperation(value = "逻辑删除信息")
	public Result<String> logicDeleteById(
			@NotBlank
			@RequestParam
			String id){
		TwbPlatform record = twbPlatformService.getById(id);
		if(null == record) {
			return Results.failure("信息不存在");
		}
		record = new TwbPlatform();
		record.setId(id);
		twbPlatformService.setUpdateInfo(record);
		record.setIsdel(1);
		boolean result = twbPlatformService.updateById(record);
		if(result) {
			return Results.success("删除成功");
		}else {
			return Results.failure("删除失败");
		}
	}
	
	
	/**
	 * @Title  logicBatchDeleteByIds
	 * @Description 批量逻辑删除
	 * @param ids
	 * @return
	 * @author DSY
	 * @date 2020-11-04 17:55:18
	 */
	
	@Auth(verifyToken = true)
	@PostMapping("/logicBatchDeleteByIds")
	@ApiOperation(value = "逻辑删除信息")
	public Result<String> logicBatchDeleteByIds(
			@NotBlank
			@RequestParam
			String ids){
		int result = twbPlatformService.logicBatchDeleteTwbPlatformByIds(ids);
		if(result>0) {
			return Results.success("删除成功");
		}else {
			return Results.failure("删除失败");
		}
	}
	
	
	/**
	 * @Title  exportExcel
	 * @Description 导出excel
	 * @param exportExcelVo
	 * @param searchVo
	 * @param request
	 * @return
	 * @author DSY
	 * @date 2020-11-04 17:55:18
	 */
	/*
	@Auth(verifyURL=false)
	@PostMapping("/exportExcel")
	@ApiOperation(value="导出excel")
	public void exportExcel(@RequestBody ExportExcelVo exportExcelVo, SearchTwbPlatformVo searchVo, HttpServletRequest request){
		ExportExcelDto dto = twbPlatformService.exportExcel(request, exportExcelVo, searchVo);
		return Results.successWithData(dto);
	}
	*/
	
	/**
	 * @Title changePlatform
	 * @Description 切换平台
	 * @param platformId
	 * @return
	 * @author DSY
	 * @date 2020年11月9日 下午3:58:54
	 */
	@Auth(verifyURL=false)
	@PostMapping("/changePlatform")
	@ApiOperation(value="切换平台")
	public Result<TwbLoginUser> changePlatform(
			@NotBlank(message = "平台ID不能为空")
			@RequestParam
			@ApiParam(value="平台ID",name="platformId",required=true)
			String platformId){
		TwbPlatform twbPlatform = twbPlatformService.getById(platformId);
		if(twbPlatform == null) {
			return Results.failure("平台信息不存在");
		}
		TwbLoginUser user = this.getLoginUser();
		user.setPlatformId(platformId);
		boolean result = twbSecurityUtil.refreshLoginUser(user, getRequest());
		if(result) {
			return Results.successWithData(user);
		}else {
			return Results.failure("平台切换失败");
		}
	}
	
	@Auth(verifyURL=false)
	@GetMapping("/listTwbPlatform")
	@ApiOperation(value="平台列表")
	public Result<List<ListTwbPlatformDto>> listTwbPlatform(ListTwbPlatformVo vo){
		List<ListTwbPlatformDto> list = twbPlatformService.listTwbPlatform(vo);
		return Results.successWithData(list);
	}
}
